import local_vuex from "@task/js/vuex";
import local_script from "@task/js/util"
import global_script from '@script/util'
import task_request from "@/axios/task";
import encrypt from '@script/encrypt'
import dateJs from "@script/date";

const task_utils = {
    /**
     * 封装发部任务所需的参数
     * @param {String} subject 任务主题
     * @param {String} describe 任务描述
     * @param {Number} issuer 发布者
     * @param {Date} deadLine 截止日期
     * @param {Array} imgList 图片清单
     */
    getIssueParams(subject, describe, issuer, deadLine, imgList) {
        let issue = local_vuex.state.issue
        let params = {
            issuer: encrypt(issuer),
            subject: encrypt(subject),
            describe: describe,
            deadLine: encrypt(deadLine),
            type: issue.type
        }
        if (imgList.length > 0) 
            params.imgList = imgList
        if (issue.dept.length > 0)
            params.deptList = global_script.parseArray(issue.dept)
        if (issue.group.length > 0) 
            params.groupList = global_script.parseArray(issue.group)
        if (issue.stu.length > 0) 
            params.stuList = global_script.parseArray(issue.stu)
        return params
    },
    /**
     * 封装查询任务所需的参数
     * @param {Number} sid 待查询对象的学号
     * @param {Number} issuer 任务发布者
     * @param {String} dept 部门名称
     */
    getQueryParams(sid, issuer, dept, close, complete) {
        let params = {}
        if (close != -1) 
            params.close = close
        if (complete != -1) 
            params.complete = complete
        if (sid != undefined) 
            params.sid = encrypt(sid)
        if (issuer != undefined)
            params.issuer = encrypt(issuer)
        if (local_vuex.state.myTask.type != -1)
            params.type = local_vuex.state.myTask.type
        if (dept !== undefined) 
            params.dept = dept
        return params;
    },
    getCommitContent(task, sid, imgs) {
        let params = {
            tid: task.uid,
            sid: encrypt(sid),
            content: task.content
        }
        if (imgs != undefined)
            params.imgs = imgs
        return params
    },
    getType(d, g, s) {
        if (d.length === 0) {
            return -1;
        } else if (g.length === 0) {
            return 0;
        } else if (s.length === 0) {
            return 1;
        } else {
            return 2;
        }
    },
    updateData(d, g, s) {
        let issue = local_vuex.state.issue;
        issue.type = this.getType(d, g, s);
        issue.dept = d;
        issue.group = g;
        issue.stu = s;
    },
    getPreviewAtt() {
        let issue = local_vuex.state.issue;
        let res = [];
        switch (issue.type) {
            case -1: return [];
            case  0: return issue.dept;
            case  1: {
                for (let i = 0; i < issue.group.length; i++) {
                    const item = issue.group[i];
                    res.push(`${issue.dept[0]} - ${item}`);
                }
                return res;
            }
            case  2: {
                for (let i = 0; i < issue.stu.length; i++) {
                    const item = issue.stu[i];
                    res.push(`${issue.dept[0]} - ${issue.group[0]} - ${item}`);
                }
                return res;
            }
        }
    },
    parseType(v) {
        switch(v) {
            case 0: return "部门";
            case 1: return "小组";
            case 2: return "个人";
        }
    },
    fillRecord(list) {
        // 当记录数有 10 条
        if (list.length === 10 ) {
            return list;
        }
        // 当记录数不足 10 条
        else {
            for (let i = list.length; i < 10; i++) {
                list.push({
                    type: -2,
                    subject: "",
                    deadLine: ""
                })                
            }
        }
        // 返回结果
        return list;
    },
    getTypeColor(v) {
        let color;
        switch(v) {
            case 0: color = "rgb(184, 57, 26)"; break;
            case 1: color = "rgb(42, 83, 157)"; break;
            case 2: color = "rgb(24, 92, 55)"; break;
        }
        return {
            background: color
        }
    },
    getLeftText(v) {
        if (v > 0) {
            return `还剩 ${v} 天`
        } else if (v == 0) {
            return "今天截止"
        } else {
            return "已截止";
        }
    },
    /**
     * 遍历列表，将当前日期与截止日期进行对比
     * 如果当前任务理论上应该已经结束
     * 但数据库查询的记录状态还是未截止
     * 则调用函数，将数据库中置为已截止，并从当前列表移除该任务
     * @param {Array} task_list 任务列表
     */
    rmClosedTask(task_list) {
        let today = dateJs.getDate("-");
        for (let index = 0; index < task_list.length; index++) {
            const item = task_list[index];
            if (dateJs.subOfDate(item.deadLine, today) < 0 && item.close == 0) {
                task_request.closeTask(item.uid);
                task_list.splice(i, 1);
            }
        }
        return task_list;
    },
    getCompleteStatus(sid, department_list = []) {
        for (let i = 0; i < department_list.length; i++) {
            const group = department_list[i].groupList;
            for (let j = 0; j < group.length; j++) {
                const student = group[j].studentList;
                for (let k = 0; k < student.length; k++) {
                    const stu = student[k];
                    if (stu.sid === sid) {
                        return stu;
                    }
                }
            }
        }
    }
}

export default task_utils;